/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = rtsp_report_data.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/04 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/rtsp/config.h"
#include "../test_rtsp_comm.h"
#include <rtsp/rtsp_core.h>
#include <rtsp/rtsp_header.h>
#include <rtsp/rtsp_comm.h>
#include <rtsp/rtsp_packet.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body = NULL;
    Frhash_header *p_st_hash_report = NULL;

    CU_ASSERT_EQUAL(test_rtsp_create_ct(&p_st_conn, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT), FR_SUCC);

    p_st_body = frreport_body_new_l7(p_st_conn);
    CU_ASSERT_PTR_NOT_NULL(p_st_body);

    p_st_hash_report = frhash2_create2(2);
    CU_ASSERT_PTR_NOT_NULL(p_st_hash_report);

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS_TITLE, "test"), FR_SUCC)

    CU_ASSERT_EQUAL(rtsp_report_data(p_st_conn, FR_YES, p_st_body, p_st_hash_report), FR_SUCC);

    Frvar_field *p_st_field = NULL;
    unsigned int ui_loop = 0;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    FRHASH_LOOP(ui_loop, p_st_hash_report, p_list_report)
    {
        if (p_list_report == NULL)
            continue;
        p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
        while (p_st_node_report)
        {
            p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
            if (p_st_report_data)
            {
                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->p_field_name, "transaction.title.s");
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "test");
            }
            p_st_node_report = FRLIST_NEXT_NODE(p_st_node_report);
        }
    }

    frhash2_free2(p_st_hash_report, list_report_node_free);
    test_rtsp_destroy_ct(&p_st_conn);
}

static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body = NULL;
    Frhash_header *p_st_hash_report = NULL;

    CU_ASSERT_EQUAL(test_rtsp_create_ct(&p_st_conn, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT), FR_SUCC);

    p_st_hash_report = frhash2_create2(2);
    CU_ASSERT_PTR_NOT_NULL(p_st_hash_report);
    CU_ASSERT_EQUAL(rtsp_report_data(p_st_conn, FR_YES, p_st_body, p_st_hash_report), FR_FAIL);

    frhash2_free2(p_st_hash_report, list_report_node_free);
    test_rtsp_destroy_ct(&p_st_conn);
}

static void __case3(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body = NULL;
    Frhash_header *p_st_hash_report = NULL;

    CU_ASSERT_EQUAL(test_rtsp_create_ct(&p_st_conn, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT), FR_SUCC);

    p_st_body = frreport_body_new_l7(p_st_conn);
    CU_ASSERT_PTR_NOT_NULL(p_st_body);

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS_TITLE, "test"), FR_SUCC)

    CU_ASSERT_EQUAL(rtsp_report_data(p_st_conn, FR_YES, p_st_body, p_st_hash_report), FR_FAIL);

    frhash2_free2(p_st_hash_report, list_report_node_free);
    test_rtsp_destroy_ct(&p_st_conn);
}

int add_suite_rtsp_report_data(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" rtsp_report_data()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
